package com.buaa.pm.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.buaa.pm.entity.MarkedParagraph;
import com.buaa.pm.entity.OssFile;
import com.buaa.pm.entity.Paper;
import com.buaa.pm.entity.Paragraph;
import com.buaa.pm.enumeration.PaperStatus;
import com.buaa.pm.feign.ParagraphClient;
import com.buaa.pm.model.PageModel;
import com.buaa.pm.model.ResultModel;
import com.buaa.pm.model.dto.ParagraphRequestDTO;
import com.buaa.pm.service.MarkedParagraphService;
import com.buaa.pm.service.OssFileService;
import com.buaa.pm.service.PaperService;
import com.buaa.pm.service.ParagraphService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/paper")
public class PaperController {

    @Autowired
    private PaperService paperService;

//    @Autowired
//    private ParagraphService paragraphService;
//    @Autowired
//    private ParagraphClient paragraphClient;
//    @Autowired
//    private OssFileService ossFileService;


    @PostMapping("/upload")
    public ResultModel<Void> uploadPaper(@RequestParam String projectId, @RequestParam MultipartFile[] files) {
        paperService.upload(projectId, files);
        return ResultModel.success();
    }

    @GetMapping("/page")
    public ResultModel<PageModel<Paper>> page(@RequestParam String projectId, @RequestParam int pageNum, @RequestParam int pageSize) {
        return ResultModel.success(paperService.pagePaper(projectId, pageNum, pageSize));
    }

    @GetMapping("/list-finish-by-project")
    public ResultModel<List<Paper>> listFinishByProject(@RequestParam String projectId) {
        return ResultModel.success(paperService.listFinishPaperByProject(projectId));
    }

    @GetMapping("/get")
    public ResultModel<Paper> getById(@RequestParam String id) {
        return ResultModel.success(paperService.getPaperById(id));
    }

    @PostMapping("/remove")
    public ResultModel<Boolean> removePaper(@RequestBody List<String> ids) {
        return ResultModel.success(paperService.removePapers(ids));
    }

    @GetMapping("/list-by-project")
    public ResultModel<List<Paper>> listByProject(@RequestParam String projectId) {
        return ResultModel.success(paperService.listPaperByProject(projectId));
    }


//    @GetMapping("/test")
//    public ResultModel<List<Paper>> test() {
//        Paper paper = paperService.getPaperById("a5cb7d08622e03539ae3ef30c98c3b2d");
//        OssFile ossFile = ossFileService.getById(paper.getTxtId());
//
//        ParagraphRequestDTO paragraphRequestDTO = new ParagraphRequestDTO();
//        paragraphRequestDTO.setFile_name(ossFile.getObjectName());
//        paragraphRequestDTO.setFile_path("txt");
//        Paragraph paragraph = new Paragraph();
//
//
//        //发送合成段提取请求
//        try{
//            JSONObject jsonObject = paragraphClient.getParagraph(paragraphRequestDTO);
//            JSONArray dataArray = jsonObject.getJSONArray("data");
//            // 处理data空
//            if(dataArray.size()==0){
//                paragraphService.removeById(paragraph);
//                paper.setStatus(PaperStatus.PARAGRAPH_FAIL);
//            }
//            else {
//                for (int i = 0; i < dataArray.size(); i++) {
//                    String dataElement = dataArray.getString(i);
//                    if(i == 0){
//                        paragraph.setContent(dataElement);
//                        paragraphService.updateById(paragraph);
//                    }
//                    else {
//                        Paragraph newParagraph = new Paragraph();
//                        newParagraph.setProjectId(paper.getProjectId());
//                        newParagraph.setPaperId(paper.getId());
//                        newParagraph.setPaperName(paper.getFilename());
//                        newParagraph.setContent(dataElement);
//                        paragraphService.save(newParagraph);
//                    }
//                }
//                //更新论文状态
//                paper.setStatus(PaperStatus.PARAGRAPH_FINISH);
//            }
//            paperService.updateById(paper);
//        }
//        catch (Exception e){
//            List<Paragraph> paragraphList = paragraphService.listParagraphByPaper(paper.getId());
//            for (Paragraph NullParagraph : paragraphList){
//                paragraphService.removeById(NullParagraph);
//            }
//            paper.setStatus(PaperStatus.PARAGRAPH_FAIL);
//            paperService.updateById(paper);
//        }
//        return ResultModel.success();
//    }
}
